const util = require("../../utils/util.js")
const app = getApp()
const {
  $Message
} = require('../../dist/base/index');

Page({
  data: {
    addresses: [],
    array: [],
    index: 0,
    startDate: '',
    startTime: '',
    endDate: '',
    endTime: '',

    startDateReset: '',
    startTimeReset: '',
    endDateReset: '',
    endTimeReset: '',

    type: ['全部', '出', '入'],
    typeIndex: 0,

    cough: ['全部', '咳嗽', '不咳嗽'],
    coughIndex: 0,

    fever: ['全部', '发烧（体温 ≧ 37.2）', '不发烧'],
    feverIndex: 0,

    carNumber: '',
    name: '',

    records: [],
    tip: "已经到底啦",
    loading: false

  },
  onLoad: function(options) {
    var that = this
    let now = new Date()
    this.setData({
      startDate: util.formatTime(now, 1),
      startTime: util.formatTime(now, 2),
      endDate: util.formatTime(now, 1),
      endTime: util.formatTime(now, 2),
      startDateReset: util.formatTime(now, 1),
      startTimeReset: util.formatTime(now, 2),
      endDateReset: util.formatTime(now, 1),
      endTimeReset: util.formatTime(now, 2),
    })
    this.getAddress()
  },
  onShow: function() {
    if (app.globalData.flushView) {
      app.globalData.flushView = false
      this.getAddress()
    }
  },
  getAddress() {
    let that = this
    let groupId = null
    if (app.globalData.group) {
      groupId = app.globalData.group._id
    }
    wx.cloud.callFunction({
      name: 'getAddress',
      data: {
        groupId: groupId
      }
    }).then(res => {
      var list = []
      for (let i = 0; i < res.result.length; i++) {
        list.push(res.result[i].address_name)
      }
      that.setData({
        array: list,
        addresses: res.result
      })
    })
  },
  formSubmit: function(e) {
    let that = this
    if (this.data.addresses.length == 0) {
      $Message({
        content: '没有出入口',
        type: 'warning',
        duration: 1
      });
      return
    }
    let item = e.detail.value
    if (!util.judgeTime(item.startDate, item.startTime, item.endDate, item.endTime)) {
      $Message({
        content: '时间段不对',
        type: 'warning',
        duration: 1
      });
      return
    }
    this.setData({
      tip: "数据加载中",
      loading: true
    })
    wx.cloud.callFunction({
        name: 'queryRegistration',
        data: {
          addressId: that.data.addresses[item.addressIndex]._id,
          startDate: item.startDate,
          startTime: item.startTime,
          endDate: item.endDate,
          endTime: item.endTime,
          typeIndex: item.typeIndex,
          coughIndex: item.coughIndex,
          feverIndex: item.feverIndex,
          carId: item.carNumber,
          name: item.name
        }
      })
      .then(res => {
        for (let i in res.result.list) {
          res.result.list[i].time = util.formatTime(new Date(res.result.list[i].time), 0)
        }
        that.setData({
          records: res.result.list,
          tip: "已经到底啦",
          loading: false
        })
      })
  },
  viewDetail(e) {
    wx.navigateTo({
      url: '/pages/record_detail/record_detail?id=' + e.currentTarget.dataset.record._id,
    })
  },
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value
    })
  },
  bindStartDateChange: function(e) {
    this.setData({
      startDate: e.detail.value
    })
  },
  bindStartTimeChange: function(e) {
    this.setData({
      startTime: e.detail.value
    })
  },
  bindEndDateChange: function(e) {
    this.setData({
      endDate: e.detail.value
    })
  },
  bindEndtTimeChange: function(e) {
    this.setData({
      endtTime: e.detail.value
    })
  },
  bindTypeChange: function(e) {
    this.setData({
      typeIndex: e.detail.value
    })
  },
  bindCoughChange: function(e) {
    this.setData({
      coughIndex: e.detail.value
    })
  },
  bindFeverChange: function(e) {
    this.setData({
      feverIndex: e.detail.value
    })
  },
  formReset: function() {
    if (this.data.addresses.length == 0) {
      $Message({
        content: '无出入口',
        type: 'warning',
        duration: 1
      });
      return
    }
    let that = this
    this.setData({
      index: 0,
      startDate: that.data.startDateReset,
      startTime: that.data.startTimeReset,
      endDate: that.data.endDateReset,
      endTime: that.data.endTimeReset,
      typeIndex: 0,
      coughIndex: 0,
      feverIndex: 0
    })
  }
})